import {mapGetters,mapMutations,mapActions} from 'vuex';
import {playMode} from "commom/js/config.js"
import {shuffle} from "commom/js/until.js"
export const playListMixin = {
  computed:{
    ...mapGetters([
      'playList'
    ])
  },
  mounted(){//当组件domready时候触发
    this.handlePlayList(this.playList);
  },
  activated(){
    this.handlePlayList(this.playList);
  },
  watch:{
    playList(newplaylist){
      this.handlePlayList(newplaylist);
    }
  },
  methods:{
    handlePlayList(){
      throw new Error('组件没有使用该方法')
    }
  }
}

export const playerMixin = {
  computed:{
    iconMode(){
      return this.playMode === playMode.sequence ? "icon-sequence-normal" : this.playMode === playMode.loop ? "icon-loop-normal" : "icon-random-normal"
    },
    ...mapGetters([
      'sequenceList',
      'currentSong',
      'playMode',
      'playList',
  ])
  },
  methods:{
    ChangeMode(){
      const mode = (this.playMode+1)%3;
      this.setPlayMode(mode);
      let list = null;
      if(mode === playMode.random){
        list = shuffle(this.sequenceList)
      }else{  
        list = this.sequenceList;
      }   
      this.resetCurrentIndex(list);
      this.setPlayList(list);
    },
    resetCurrentIndex(list){
      let index = list.findIndex((item)=>{
        return item.id === this.currentSong.id
      })     
      this.setCurrentIndex(index);  
    },
    ...mapMutations({
      setPlayingState:'SET_PLAYING_STATE',
      setCurrentIndex:'SET_CURRENT_INDEX',
      setPlayMode:'SET_PLAY_MODE',
      setPlayList:"SET_PLAYLIST"
    })
  }
}

export const searchMixin = {
  data(){
    return{
      querry:"",
      refreshDelay:100
    }
  },
  computed:{
    ...mapGetters([
      'searchHistory'
    ])
  },
  methods:{
    listScroll(){//滑动时使焦点消失
      this.$refs.searchbox.blur();
    },
    saveSearch(){//保存搜索记录到缓存
      this.saveSearchHistory(this.querry)
    },
    onQuerryChange(querry){
      this.querry = querry
    },
    setquerry(querry){
      this.$refs.searchbox.setQuerry(querry)
    },
    deleteOne(item){//删除一条记录
      this.deleteSearchHistory(item)
    },
    ...mapActions([
      'saveSearchHistory',
      'deleteSearchHistory'
    ])
  }
}